
-- DESPUES DE INGRESAR INFROMACION DE LOS PACIENTES EJECUTAR ESTO:

SELECT * FROM HM_EVALUACION WHERE  IDDETALLEENFERMEDAD IN (39,48,45) AND VALOR1 <>''

UPDATE HM_EVALUACION SET VALOR2 = CASE WHEN  LTRIM(RTRIM( VALOR1))= 'SI' THEN '1' ELSE '0' END 
WHERE  IDDETALLEENFERMEDAD IN (39,48,45) AND VALOR1 <>''

----------------------------------------------------------------------------------------------


SELECT * FROM [dbo].[HM_PACIENTE]
SELECT * FROM HM_DETALLEENFERMEDAD WHERE USOSISTEMAINTELIGENTE = 'SI'

SELECT		--VALOR1, VALOR2,* 
			HC.IDHISTORIACLINICA,IDPACIENTE, NUMEROHISTORIA,SINDROMEMETABOLICO,COUNT(*) NUM
FROM		[dbo].[HM_HISTORIACLINICA] HC
INNER JOIN [dbo].[HM_EVALUACION] EV ON HC.IDHISTORIACLINICA = EV.IDHISTORIACLINICA
INNER JOIN [dbo].[HM_DETALLEENFERMEDAD] DENF ON EV.IDDETALLEENFERMEDAD = DENF.IDDETALLEENFERMEDAD
WHERE DENF.USOSISTEMAINTELIGENTE = 'SI'
		AND (RTRIM(LTRIM( VALOR1)) <>'' OR VALOR2 in ('0','1'))
		AND RTRIM(LTRIM( VALOR1)) <> '- ////' 
GROUP BY HC.IDHISTORIACLINICA,IDPACIENTE, NUMEROHISTORIA,SINDROMEMETABOLICO
HAVING COUNT(*) >=0



SELECT VALOR1,VALOR2, * FROM  HM_EVALUACION EVAL
INNER JOIN [dbo].[HM_DETALLEENFERMEDAD] DE ON EVAL.IDDETALLEENFERMEDAD = DE.IDDETALLEENFERMEDAD
WHERE	USOSISTEMAINTELIGENTE = 'SI'
		AND IDEVALUACION NOT IN (

SELECT		IDEVALUACION
			--HC.IDHISTORIACLINICA,IDPACIENTE, NUMEROHISTORIA,SINDROMEMETABOLICO,COUNT(*) NUM
FROM		[dbo].[HM_HISTORIACLINICA] HC
INNER JOIN [dbo].[HM_EVALUACION] EV ON HC.IDHISTORIACLINICA = EV.IDHISTORIACLINICA
INNER JOIN [dbo].[HM_DETALLEENFERMEDAD] DENF ON EV.IDDETALLEENFERMEDAD = DENF.IDDETALLEENFERMEDAD
WHERE DENF.USOSISTEMAINTELIGENTE = 'SI'
		AND (RTRIM(LTRIM( VALOR1)) <>'' OR VALOR2 in ('0','1'))
		AND RTRIM(LTRIM( VALOR1)) <> '- ////' )